/*
 * EditorOps.java
 * 
 * Created by demory on Feb 6, 2010, 10:56:06 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.cart.modules;

import org.fpdev.apps.cart.CartEvent;
import org.fpdev.apps.cart.CartEventResult;
import org.fpdev.apps.cart.CartModule;
import org.fpdev.apps.cart.Cartographer;
import org.fpdev.apps.cart.Editor;
import org.fpdev.apps.cart.Editor.Property;

/**
 *
 * @author demory
 */
public class EditorOps extends CartModule {

  public EditorOps(Cartographer cart) {
    super(cart);
  }

  public ID getID() {
    return ID.SYSTEM;
  }

  public CartEventResult handleEvent(CartEvent event) {
    Editor ed = event.getEditor();
    CartEventResult result = new CartEventResult();
    
    if(ed == null) {
      cart_.getGUI().msg("Error handling "+event+" event: No active editor");
      result.setStatus(CartEventResult.Status.FAILURE);
      return result;
    }
    
    switch(event.getType()) {
      case REFRESH_CANVAS:
        ed.getFrame().getCanvas().repaint();
        result.setStatus(CartEventResult.Status.SUCCESS);
        break;
      case EDIT_UNDO:
        ed.getHistory().undoLast();
        ed.getFrame().getCanvas().repaint();
        result.setStatus(CartEventResult.Status.SUCCESS);
        break;
      case EDIT_REDO:
        ed.getHistory().redoLast();
        ed.getFrame().getCanvas().repaint();
        result.setStatus(CartEventResult.Status.SUCCESS);
        break;
      case ZOOM_IN:
        ed.getFrame().getCanvas().zoom(.1);
        result.setStatus(CartEventResult.Status.SUCCESS);
        break;
      case ZOOM_OUT:
        ed.getFrame().getCanvas().zoom(-.1);
        result.setStatus(CartEventResult.Status.SUCCESS);
        break;
      case MODIFY_EDITOR_PROPERTY:
        Editor.Property prop = (Property) event.getProperty("property");
        String value = event.getStringProperty("value");
        ed.setProperty(prop, value);
        ed.getFrame().getCanvas().repaint();
        result.setStatus(CartEventResult.Status.SUCCESS);
        break;
    }

    return result;
  }
}
